package net.lzzy.practices.services;


import net.lzzy.practices.entities.User;
import net.lzzy.practices.repositories.UserProjection;
import net.lzzy.practices.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import  org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * @author  Jaymo 2022/5/9  rescrip by5/12
 */

@Service
public class UserServicelmpl implements  UserService{

    private  final UserRepository repository;

    @Autowired
    public UserServicelmpl(UserRepository repository) {
        this.repository = repository;
    }

    @Override
    public  User check(String userName, String password) {
        return  repository.findUserByUserNameAndPassword(userName,password);
    }

    @Override
    public  boolean isUserNameOccupied(String userName) {
        return  repository.countByUserName(userName)  > 0;
    }

    @Override
    public  User register(User user)  {
        return  repository.save(user);
    }

    @Override
    public List<User> get() {return  repository.findAllByOrderByUpdateTimeDesc();}

    @Override
    public  List<User> get(int page, int size) {
        return repository.findAllByOrderByUpdateTimeDesc(PageRequest.of(page,size));
    }

    @Override
    public  Integer count() {return  repository.countAllBy();}

    @Override
    public  List<User> search(String kw)  {return  repository.findByQuery(kw);}

    @Override
    public  List<User> getApplying(boolean applying) {return  repository.findUserByApplyTeacherOrderByUpdateTimeDesc(applying); }


    @Override
    public  User approveTeacher(UUID id){
        //findById是eager， getById 是延迟查询,事务处理
        User user = repository.findById(id).orElse(null);
        if (user !=null) {
            user.setType(User.TYPE_TEACHER);
            user.setApplyTeacher(false);
            return  repository.save(user);
        }
        return  null;
    }

    @Override
    public User declineTeacher(UUID id) {
        User user = repository.findById(id).orElse(null);
        if (user !=null){
            user.setApplyTeacher(false);
            return  repository.save(user);
        }
        return  null;
    }

    @Override
    public User banUser(UUID id) {
        User user = repository.findById(id).orElse(null);
        if (user !=null) {
            user.setType(User.TYPE_BANNED);
            user.setApplyTeacher(false);
            return   repository.save(user);
        }
        return null;
    }

    @Override
    public String getSalt(String userName) {
        List<UserProjection> salts = repository.findUserByUserName(userName);
        if (salts == null|| salts.size()==0){
            return "";
        }
        UserProjection projection = salts.get(0);
        if (projection == null){
            return "";
        }
        String salt = projection.getSalt();
        return  salt ==null?"":salt;

    }

    @Override
    public User getById(UUID fromString) {
        return null;
    }

}
